package com.mspbots.core.statistic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.teams.service.MessagesService;
import com.mspbots.core.teams.service.TeamsUserService;
import com.mspbots.core.ticketai.service.TicketService;
import com.mspbots.core.wise.service.TenantUserMappingService;
import com.mspbots.core.wise.service.TenantUserService;
import com.mspbots.dto.statistics.MessagesStatisticDTO;
import com.mspbots.dto.statistics.MessagesStatisticUserDTO;
import com.mspbots.dto.statistics.StatusStatisticDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * @ClassName com.mspbots.core.statistic.StatisticController
 * @Description TODO
 * @Author Jun
 * @Date 2019/8/21 15:05
 * @Version 1.0
 **/
@RestController
@RequestMapping("statistic")
public class StatisticController {

    @Autowired
    private TicketService ticketService;
    @Autowired
    private MessagesService messagesService;
    @Autowired
    private TenantUserService tenantUserService;
    @Autowired
    private TeamsUserService tenantTeamsUserService;
    @Autowired
    private TenantUserMappingService tenantUserMappingService;


    @GetMapping("/count-everyday")
    public Callable countEveryday(Long tenantId) {
        return () -> ticketService.countEveryday(tenantId);

    }

    @GetMapping("/count-by-type")
    public Callable countByType(Long tenantId) {
        return () -> messagesService.countByType(tenantId);
    }

    @GetMapping("/count-user")
    public Callable countUser(Long tenantId) {
        QueryWrapper<com.mspbots.core.wise.model.TenantUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", tenantId);
        queryWrapper.eq("enable", true);
        return () -> tenantUserService.count(queryWrapper);
    }

    @GetMapping("/count-by-day")
    public Callable countByDay(MessagesStatisticDTO messagesStatisticDTO) {
        Assert.notNull(messagesStatisticDTO.getDateFrom(), "Parameter 'FromDate' cannot be empty!");
        Assert.notNull(messagesStatisticDTO.getDateTo(), "Parameter 'ToDate' cannot be empty!");
        return () -> messagesService.countByDay(messagesStatisticDTO);
    }

    @GetMapping("/count-by-day-user")
    public Callable countByDayUser(Page<MessagesStatisticUserDTO> page, MessagesStatisticUserDTO messagesStatisticUserDTO) {
        Assert.notNull(messagesStatisticUserDTO.getDateFrom(), "Parameter 'FromDate' cannot be empty!");
        Assert.notNull(messagesStatisticUserDTO.getDateTo(), "Parameter 'ToDate' cannot be empty!");
        return () -> messagesService.countByDayUser(page, messagesStatisticUserDTO);
    }

    @GetMapping("/count-status")
    public Callable countStatus(StatusStatisticDTO statusStatisticDTO) {
        return () -> {
            QueryWrapper qwTenantUser = new QueryWrapper();
            qwTenantUser.eq("tenant_id", statusStatisticDTO.getTenantId());
            statusStatisticDTO.setCwUserNum(tenantUserService.count(qwTenantUser));

            statusStatisticDTO.setTeamsUserNum(tenantTeamsUserService.count(qwTenantUser));
            QueryWrapper<com.mspbots.core.wise.model.TenantUserMapping> qwUserMapping = new QueryWrapper<>();
            qwUserMapping.isNotNull("tenant_user_id");
            qwUserMapping.isNotNull("teams_user_id");
            qwUserMapping.eq("tenant_id", statusStatisticDTO.getTenantId());
            statusStatisticDTO.setMappingUserNum(tenantUserMappingService.count(qwUserMapping));
            statusStatisticDTO.setMessageNum(messagesService.countByGroupUser(statusStatisticDTO.getTenantId()));
            return statusStatisticDTO;
        };
    }


}
